//
//  YGSearchController.m
//  SunshineMall
//
//  Created by 张艳江 on 2018/11/1.
//  Copyright © 2018年 张艳江. All rights reserved.
//

#import "SCSearchController.h"
#import "SCSearchTagView.h"
#import "SCSearchResultController.h"
#import "SCSearchTitleView.h"

@interface SCSearchController ()<UITextFieldDelegate>

@property (nonatomic, strong) SCSearchTitleView *searchTitleView;//搜索栏
@property (nonatomic, strong) NSMutableArray    *hotArray;
@property (nonatomic, strong) NSMutableArray    *historyArray;
@property (nonatomic, strong) SCSearchTagView   *searchTagView;

@end

@implementation SCSearchController

- (NSMutableArray *)historyArray{
    if (!_historyArray) {
        _historyArray = [NSKeyedUnarchiver unarchiveObjectWithFile:KHistorySearchPath];
        if (!_historyArray) {
            self.historyArray = [NSMutableArray array];
        }
    }
    return _historyArray;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!_searchTitleView.searchTextField.isFirstResponder) {
        [self.searchTitleView.searchTextField becomeFirstResponder];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGColor;
    //设置搜索
    [self setupSearchTitleView];
    //搜索标签
    [self setupSearchTagView];
}
- (void)setupSearchTitleView{
    self.searchTitleView = [[SCSearchTitleView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight + 7, kScreenW - 15 - 7 - 44, 30)];
    self.searchTitleView.searchTextField.delegate = self;
    self.navigationItem.titleView = self.searchTitleView;
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithButtonFrame:CGRectMake(0, 0, 44, 44) title:@"取消" color:[UIColor blackColor] font:SCFont(15) target:self action:@selector(clickCancel)];
}
- (void)clickCancel{
    [self.searchTitleView.searchTextField resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setupSearchTagView{
    
    self.hotArray = [NSMutableArray arrayWithObjects:@"铅笔", @"书包", @"男鞋", @"女鞋", nil];
    self.searchTagView = [[SCSearchTagView alloc] initWithFrame:CGRectMake(0, 0.5, kScreenW, kScreenH - kTopHeight) hotArray:self.hotArray historyArray:self.historyArray];
    WeakSelf;
    self.searchTagView.tapAction = ^(NSString *str) {
        [weakSelf.searchTitleView.searchTextField resignFirstResponder];
        [weakSelf pushToSearchResultWithSearchStr:str];
    };
    [self.view addSubview:self.searchTagView];
}
#pragma mark - searchTextField的代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        [SVProgressHUD showImage:nil status:@"请输入搜索内容"];
        return NO;
    }
    [textField resignFirstResponder];
    [self pushToSearchResultWithSearchStr:textField.text];
    return YES;
}
- (void)pushToSearchResultWithSearchStr:(NSString *)str{
    
    [self setHistoryArrWithStr:str];
    self.searchTitleView.searchTextField.text = str;
    SCSearchResultController *searchResultVc = [[SCSearchResultController alloc]init];
    searchResultVc.isGiftApp = self.isGiftApp;
    searchResultVc.searchStr = str;
    searchResultVc.hotArray = [self.hotArray copy];
    [self.navigationController pushViewController:searchResultVc animated:YES];
}
- (void)setHistoryArrWithStr:(NSString *)str{
    for (int i = 0; i < self.historyArray.count; i++) {
        if ([self.historyArray[i] isEqualToString:str]) {
            [self.historyArray removeObjectAtIndex:i];
            break;
        }
    }
    [self.historyArray insertObject:str atIndex:0];
    [NSKeyedArchiver archiveRootObject:self.historyArray toFile:KHistorySearchPath];
}

@end
